import { _decorator, Component, Animation, RigidBody2D, ERigidBody2DType, CircleCollider2D, Sprite, UITransform, AnimationClip, SpriteFrame } from 'cc';
import { StateMachine } from './stateMachine';
import { DIRECTION_ORDER_ENUM, DIRECTION_UNUM, ENTITY_STATE_ENUM, PARAMS_NAME_ENUM, TILE_TYPE_ENUM } from '../scripts/enums';
import { BallCrtl } from '../scripts/player/ballCrtl';
const { ccclass, property } = _decorator;

@ccclass('EntityManager')
export class EntityManager extends Component {
    _fsm: StateMachine = null;

    private _direction: DIRECTION_UNUM;
    private _state: ENTITY_STATE_ENUM;

    get direction() { return this._direction; }

    set direction(new_direction: DIRECTION_UNUM) {
        this._direction = new_direction;
        this._fsm.setParams(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction]);
    }

    // 数据与 UI 分离
    get state() { return this._state }

    set state(newState: ENTITY_STATE_ENUM) {
        if (this._state === newState) return;
        
        this._state = newState;
        this._fsm.setParams(this._state, true);
    }

    init() {
        const sprite = this.addComponent(Sprite);
        sprite.sizeMode = Sprite.SizeMode.CUSTOM;
        this.getComponent(UITransform).setContentSize(200, 200);

        // 添加钢体
        const rigidbody = this.addComponent(RigidBody2D);
        rigidbody.type = ERigidBody2DType.Dynamic;
        rigidbody.fixedRotation = true; // 固定角速度
        // 添加形状
        const phy = this.addComponent(CircleCollider2D);
        phy.radius = 30;
        // 添加ball控制移动
        this.addComponent(BallCrtl);
    }
}
